
#import "NexusClient.h"

//
// singleton
//
static NexusClient*	globalClient											= nil;

@implementation NexusClient

//
// nexus php
//
@synthesize cookieNexusPhp													= _cookieNexusPhp;

//
// domain
//
@synthesize domain															= _domain;

//
// protocol
//
@synthesize protocol														= _protocol;

//
// singleton
//
+ (NexusClient*)sharedClient
{
	if(!globalClient)
		globalClient														= [[NexusClient alloc] init];
	
	return globalClient;
}

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// setup default value
	//
	self.protocol															= @"https";
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.cookieNexusPhp														= nil;
	self.domain																= nil;
	self.protocol															= nil;
	
	[super dealloc];
}

//
// base url
//
- (NSString*)baseURLString
{
	return [NSString stringWithFormat:@"%@://%@/", self.protocol, self.domain];
}

//
// torrent download link
//
- (NSString*)torrentDownloadAddress:(TorrentInfo*)torrentInfo passKey:(NSString*)passKey httpsMode:(BOOL)httpsMode
{
	return httpsMode ? [NSString stringWithFormat:@"https://%@/download.php?id=%d&passkey=%@&https=1", self.domain, torrentInfo.torrentId, passKey] : [NSString stringWithFormat:@"http://%@/download.php?id=%d&passkey=%@", self.domain, torrentInfo.torrentId, passKey];
}

//
// get login form
//
- (void)getLoginFormWithCallback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetLoginFormRequest* httpRequest										= [[GetLoginFormRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	[httpRequest startWithCallback:httpCallback];
}

//
// get captcha image
//
- (void)getCaptchaImage:(NSString*)address callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	NexusHttpRequest* httpRequest											= [[NexusHttpRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.relPath														= address;
	httpRequest.protocol													= self.protocol;
	[httpRequest startWithCallback:httpCallback];
}

//
// post login
//
- (void)postLogin:(NSString*)userName password:(NSString*)password captcha:(NSString*)captcha sessionId:(NSString*)sessionId callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	PostLoginRequest* httpRequest											= [[PostLoginRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.userName													= userName;
	httpRequest.password													= password;
	httpRequest.captcha														= captcha;
	httpRequest.cookiePHPSessionId											= sessionId;
	[httpRequest startWithCallback:httpCallback];
}

//
// get user details
//
- (void)getUserDetails:(NSInteger)userId callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetUserDetailsRequest* httpRequest										= [[GetUserDetailsRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.userDetails.userId											= userId;
	[httpRequest startWithCallback:httpCallback];
}

//
// get user passkey
//
- (void)getUserPassKeyWithCallback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetUserPassKeyRequest* httpRequest										= [[GetUserPassKeyRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	[httpRequest startWithCallback:httpCallback];
}

//
// get torrent list
//
- (void)getTorrentListPage:(NSInteger)pageIndex callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetTorrentListRequest* httpRequest										= [[GetTorrentListRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.pageIndex													= pageIndex;
	[httpRequest startWithCallback:httpCallback];
}

//
// get user torrents list
//
- (void)getUserTorrentList:(NSInteger)userId type:(NSString*)type callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetUserTorrentListRequest* httpRequest									= [[GetUserTorrentListRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.userId														= userId;
	httpRequest.type														= type;
	[httpRequest startWithCallback:httpCallback];
}

//
// get file list
//
- (void)getTorrentFileList:(NSInteger)torrentId callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetTorrentFileListRequest* httpRequest									= [[GetTorrentFileListRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.torrentId													= torrentId;
	[httpRequest startWithCallback:httpCallback];
}

//
// get peer list
//
- (void)getTorrentPeerList:(NSInteger)torrentId callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetTorrentPeerListRequest* httpRequest									= [[GetTorrentPeerListRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.torrentId													= torrentId;
	[httpRequest startWithCallback:httpCallback];
}

//
// get torrent details
//
- (void)getTorrentDetails:(NSInteger)torrentId callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetTorrentDetailsRequest* httpRequest									= [[GetTorrentDetailsRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.torrentInfo.torrentId										= torrentId;
	[httpRequest startWithCallback:httpCallback];
}

//
// search torrent
//
- (void)searchTorrentWithPattern:(NSString*)filterPattern pageIndex:(NSInteger)pageIndex callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	GetTorrentListRequest* httpRequest										= [[GetTorrentListRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.pageIndex													= pageIndex;
	httpRequest.filterPattern												= filterPattern;
	[httpRequest startWithCallback:httpCallback];
}

//
// compute required karma count for magic
//
- (void)getRequiredKarmaForTorrent:(NSInteger)torrentId magicType:(NSInteger)magicType userType:(NSInteger)userType duration:(NSInteger)duration callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	TorrentMagicRequest* httpRequest										= [[TorrentMagicRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.duration													= duration;
	httpRequest.magicType													= magicType;
	httpRequest.testMode													= YES;
	httpRequest.torrentId													= torrentId;
	httpRequest.userType													= userType;
	[httpRequest startWithCallback:httpCallback];
}

//
// torrent magic
//
- (void)doMagic:(NSInteger)magicType forTorrent:(NSInteger)torrentId userType:(NSInteger)userType duration:(NSInteger)duration callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	TorrentMagicRequest* httpRequest										= [[TorrentMagicRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.duration													= duration;
	httpRequest.magicType													= magicType;
	httpRequest.testMode													= NO;
	httpRequest.torrentId													= torrentId;
	httpRequest.userType													= userType;
	[httpRequest startWithCallback:httpCallback];
}

//
// exchange uCoin -> karma
//
- (void)exchangeUCoinToKarma:(NSInteger)count callback:(AsyncHttpRequestFinishCallback)callback
{
	callback																= [[callback copy] autorelease];
	id httpCallback = ^void(AsyncHttpRequest* httpRequest)
	{
		if(callback)
			callback(httpRequest);
		
		[httpRequest release];
	};
	
	KarmaShopRequest* httpRequest											= [[KarmaShopRequest alloc] init];
	httpRequest.domain														= self.domain;
	httpRequest.protocol													= self.protocol;
	httpRequest.cookieNexusPhp												= self.cookieNexusPhp;
	httpRequest.eventType													= 1000;
	httpRequest.count														= count;
	[httpRequest startWithCallback:httpCallback];
}

//
// parse free type
//
+ (NSString*)parseFreeTypeForImgClass:(NSString*)imgClass
{
	if(!imgClass)
		return nil;
	
	if([imgClass isEqualToString:@"pro_free"])
		return @"Free";
	
	if([imgClass isEqualToString:@"pro_2up"])
		return @"2x";
	
	if([imgClass isEqualToString:@"pro_free2up"])
		return @"2xFree";
	
	if([imgClass isEqualToString:@"pro_50pctdown"])
		return @"50%";
	
	if([imgClass isEqualToString:@"pro_50pctdown2up"])
		return @"2x50%";
	
	if([imgClass isEqualToString:@"pro_30pctdown"])
		return @"30%";
	
	return nil;
}

//
// parse free type
//
+ (NSString*)parseFreeTypeForFontClass:(NSString*)fontClass
{
	if(!fontClass)
		return nil;
	
	if([fontClass isEqualToString:@"free"])
		return @"Free";
	
	if([fontClass isEqualToString:@"twoup"])
		return @"2x";
	
	if([fontClass isEqualToString:@"twoupfree"])
		return @"2xFree";
	
	if([fontClass isEqualToString:@"halfdown"])
		return @"50%";
	
	if([fontClass isEqualToString:@"twouphalfdown"])
		return @"2x50%";
	
	if([fontClass isEqualToString:@"thirtypercent"])
		return @"30%";
	
	return nil;
}

//
// parse free type
//
+ (NSString*)parseFreeTypeForRowClass:(NSString*)rowClass
{
	if(!rowClass)
		return nil;
	
	if([rowClass isEqualToString:@"free_bg"])
		return @"Free";
	
	if([rowClass isEqualToString:@"twoup_bg"])
		return @"2x";
	
	if([rowClass isEqualToString:@"twoupfree_bg"])
		return @"2xFree";
	
	if([rowClass isEqualToString:@"halfdown_bg"])
		return @"50%";
	
	if([rowClass isEqualToString:@"twouphalfdown_bg"])
		return @"2x50%";
	
	if([rowClass isEqualToString:@"thirtypercentdown_bg"])
		return @"30%";
	
	return nil;
}

@end
